import { create, update, remove, query } from '../../../../framework/service/index';
import { ToastTips } from '../../../../common/toastTips/tosat'

export default {

  namespace: 'search',

  state: {
    jobList: [],
    pageNum: 1,
    pageSize:10,
    pages: 0,
    ordeyIndex:0,
    areaIndex:0,
    typeNameIndex:0,
    isShow: -1,
    hiddenArr: [],
    selectIndex:1,
    ordey:[
      {name: '综合排序',id:1,value:''},
      {name: '最新发布',id:2,value:'publishtime'},
      {name: '热门兼职',id:3,value:'view'},
      {name: '附近兼职',id:4,value:'nearby'}
    ],
    key:'',
    typeId:'',
    district :'',
    screenTabIndex:0,
    searchValue:''
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(location => {
        if(location.pathname === '/search') {
          // dispatch({type: 'query'});
          dispatch({type: 'relations/getTypes'});
          dispatch({type: 'relations/getAreas'});
          dispatch({
            type: 'querySuccess',
            payload:{
              isShow:-1,
              // ordeyIndex:0,
              // areaIndex:0,
              // typeNameIndex:0,
              // key:'',
              // typeId:'',
              // district :'',
            }
          })
        }
      })
    },
  },

  effects: {
    // 获取兼职类型列表；
    *query({ payload }, { call, put, select }) {

      let { pageSize,key,typeId,district } = yield select (({search}) => search);

      const { cityName } = yield select (({relations}) => relations);
      const city = cityName.slice(0,cityName.length-1);
      const pageNum = payload && payload.pageNum ? payload.pageNum : 1;

      if(typeId == '0'){
        typeId = '';
      }
      if(district == '区域'){
        district = '';
      }

      const { code, data, message } = yield call(query, '/api/pub/jobs/types/all', {});
      // console.log('兼职类型',code);
      const typeArr = yield select(({search}) => search.typeArr);

      // console.log('code', JSON.stringify(code));
      if (code === 200 && data) {
        // console.log('兼职类型',data);
        for (let i=0;i<data.length;i++) {
          typeArr.push(data[i])
        }
        // console.log('兼职类型数组',typeArr);
        yield put({
          type: 'querySuccess',
          payload: {
            typeArr: typeArr
          }
        })
      }
    },

    // 点击搜索按钮，获取兼职内容；
    *onSearch({ payload }, { call, put, select }) {

      let { pageSize,key,typeId,district,searchValue } = yield select (({search}) => search);

      if(searchValue == ""){
        ToastTips('请输入兼职名称', 1500, null);
        return;
      }

      const { cityName } = yield select (({relations}) => relations);
      const city = cityName.slice(0,cityName.length-1);
      let pageNum = payload && payload.pageNum ? payload.pageNum : 1;

      if(typeId == '0'){
        typeId = '';
      }
      if(district == '区域'){
        district = '';
      }

      // let pageNum = yield select(({search})=>search.pageNum)
      // let pageSize = yield select(({search})=>search.pageSize)

      // if ( payload != undefined ) {
      //   pageNum = payload.pageNum,
      //   pageSize = payload.pageSize
      // }

      const { code, data, message } = yield call(query, '/api/pub/jobs', {
        title:searchValue,
       /* typeName:searchValue,*/
        pageNum,
        pageSize,
        key,
        typeId,
        district,
        city,
      });
      // console.log('code', JSON.stringify(code));
      // console.log('data', JSON.stringify(data));
      // console.log('message', JSON.stringify(message));

      if (code === 200 && data) {
        // console.log('搜索结果', JSON.stringify(data));
        yield put({
          type: 'querySuccess',
          payload: {
            jobList: data.records
          }
        })
      }
    },

    *getTypes({ payload }, { call, put, select }) {
      const { code, data, message } = yield call(query, '/api/pub/jobs/types/all', {});
      // console.log('兼职类型',code);
      const typeArr = yield select(({search}) => search.typeArr);

      // console.log('code', JSON.stringify(code));
      if (code === 200 && data) {
        // console.log('兼职类型',data);
        for (let i=0;i<data.length;i++) {
          typeArr.push(data[i])
        }
        // console.log('兼职类型数组',typeArr);
        yield put({
          type: 'querySuccess',
          payload: {
            typeArr: typeArr
          }
        })
      }
    },

    *getJob({ payload }, { call, put, select }) {

      console.log('getJobgetJobgetJobgetJob',payload);
      const { code, data, message } = yield call(query, '/api/jobs', {});
      if ( code == 200 && data ) {
        console.log('getJobgetJobgetJobgetJob搜索数据',data);
        yield put({
            type: 'querySuccess',
            payload: {
              jobList: data.records
            }
        })
      }
    }
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload };
    },
  }

};
